package com.meta.menu.service.impl;

import com.meta.common.CommonInfo;
import com.meta.common.ConstDef;
import com.meta.menu.bo.Menu;
import com.meta.menu.service.MenuService;
import com.meta.nd.service.NvService;
import com.meta.nd.vo.Nv;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    NvService nvService;
    @Override
    public Menu getRootMenu(String menuCode) {
        Nv rootNv = this.nvService.getNvByCode(CommonInfo.getDefault(),menuCode,ConstDef.FD_NAME_CODE,ConstDef.ND_CODE_MENU);
        Menu menu = transferNvToMenu(rootNv);
        return menu;
    }
    Menu transferNvToMenu(Nv nv){
        Menu menu = new Menu();
        menu.setCode(nv.getRawStringByName(ConstDef.FD_NAME_CODE));
        menu.setDescription(nv.getRawStringByName(ConstDef.FD_NAME_DESCRIPTION));
        menu.setLeaf(nv.getRawBoolByName(ConstDef.FD_NAME_LEAF));
        menu.setMd(
                nv.getRawStringByName(ConstDef.FD_NAME_MODULE_CODE),
                nv.getRawStringByName(ConstDef.FD_NAME_MD_CODE),
                nv.getRawStringByName(ConstDef.FD_NAME_MV_CODE),
                nv.getRawBoolByName(ConstDef.FD_NAME_LEAF));
        menu.setModuleCode(nv.getRawStringByName(ConstDef.FD_NAME_MODULE_CODE));

        for( Nv childNv : nv.getChildNvs(ConstDef.ND_NAME_MENU)){
            Menu childMenu = transferNvToMenu(childNv);
            menu.getChildren().add(childMenu);
        }
        return menu;
    }
}
